package com.dx.mes.service.special.service;

import com.dx.common.table.ppt.RetSlotDef;
import com.dx.common.table.ppt.Ret_ft_rule;
import com.dx.mes.common.dao.IBisMdlDefDao;
import com.dx.mes.common.dao.IRetFtRulDao;
import com.dx.mes.common.dao.IRetSlotDefDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.special.service.IFspfutactionService;
import com.dx.mes.facade.special.tx.Fspfutaction.FspfutationI;
import com.dx.mes.facade.special.tx.Fspfutaction.FspfutationIA;
import com.dx.mes.facade.special.tx.Fspfutaction.FspfutationO;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.io.InputStream;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import static com.dx.mes.common.bean.def.GenericDef.*;

@Service("fspfutactionService")
public class FspfutactionService implements IFspfutactionService{
	private LogUtils logUtils;
	private static String TX_NAME = "FSPFUTACTION";

	@Autowired
	private IBisMdlDefDao bisMdlDefDao;
	@Autowired
	private IRetFtRulDao retFutactionDao;

	@Autowired
	private IRetSlotDefDao retSlotDefDao;
	
	public String subMainProc(String evt_no,String strInMsg) {
		AppContext.clear();
		AppContext.setCurrServiceName(FspfutactionService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FspfutactionService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");

		String outMsg = "";
		FspfutationI inTrx = new FspfutationI();
		FspfutationO outTrx = new FspfutationO();

		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

		try {
			inTrx = JacksonUtil.fromJson(strInMsg, FspfutationI.class);

			subMainProc2(inTrx.getAction_flg().toCharArray()[0],inTrx,outTrx);
			if (!outTrx.getRtn_code().equals(RETURN_CODE_OK)) {
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
			} else {
				bisMdlDefDao.flushCurSession();
			}
		} catch (Exception ex) {
			outTrx.setRtn_mesg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logUtils.error(Util.stackTraceToString(ex));
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
		} finally {
			outMsg = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + outTrx + "]");
		}
		return outMsg;
	}

	private void subMainProc2(char actionType,FspfutationI inTrx,FspfutationO outTrx) {

		switch (actionType) {
			case 'Q' :
				queryReserListByConditions(inTrx,outTrx);
				break;
			case 'A' :
				registerByRes(inTrx,outTrx);
				break;
			case 'Y' :
				active(inTrx,outTrx);
				break;
			case 'D' :
				deleteBySeqId(inTrx,outTrx);
				break;
			default: {
				String gszMsgBuf = "Invalide type input: [" + actionType + "]";
				outTrx.setRtn_code(String.valueOf(E_FIPINQSHT_INVALID_INPUT));
				outTrx.setRtn_mesg(gszMsgBuf);
				return;
			}
		}
	}



	private void queryReserListByConditions(FspfutationI inTrx, FspfutationO outTrx) {

			FspfutationIA fspfutationIA = inTrx.getIary().get(0);
			String deft_code = fspfutationIA.getDeft_code();
			String group_id_fk = fspfutationIA.getGroup_id_fk();
			String mdl_id_fk = fspfutationIA.getMdl_id_fk();
			String ope_id_fk = fspfutationIA.getOpe_id_fk();
			String ope_no_fk = fspfutationIA.getOpe_no_fk();
			String path_id_fk = fspfutationIA.getPath_id_fk();
			String path_ver_fk = fspfutationIA.getPath_ver_fk();
			String prd_grade = fspfutationIA.getPrd_grade();
			String rsn_code = fspfutationIA.getRsn_code();
			String rul_act_code = fspfutationIA.getRul_act_code();
			String tool_id_fk = fspfutationIA.getTool_id_fk();
			String wo_id_fk = fspfutationIA.getWo_id_fk();
			String hql = "select * from ret_ft_rule where 1 = 1";
			if (StringUtils.isNotBlank(deft_code)) {
				hql += " and deft_code ='"+deft_code+"'";
			}
			if (StringUtils.isNotBlank(group_id_fk)) {
				hql += " and group_id_fk ='"+group_id_fk+"'";
			}
			if (StringUtils.isNotBlank(mdl_id_fk)) {
				hql += " and mdl_id_fk ='"+mdl_id_fk+"'";
			}
			if (StringUtils.isNotBlank(ope_no_fk)) {
				hql += " and ope_no_fk ='"+ope_no_fk+"'";
			}
			if (StringUtils.isNotBlank(ope_id_fk)) {
				hql += " and ope_id_fk ='"+ope_id_fk+"'";
			}
			if (StringUtils.isNotBlank(path_id_fk)) {
				hql += " and path_id_fk ='"+path_id_fk+"'";
			}
			if (StringUtils.isNotBlank(path_ver_fk)) {
				hql += " and path_ver_fk ='"+path_ver_fk+"'";
			}
			if (StringUtils.isNotBlank(rsn_code)) {
				hql += " and rsn_code ='"+rsn_code+"'";
			}
			if (StringUtils.isNotBlank(prd_grade)) {
				hql += " and prd_grade ='"+prd_grade+"'";
			}
			if (StringUtils.isNotBlank(rul_act_code)) {
				hql += " and rul_act_code ='"+rul_act_code+"'";
			}
			if (StringUtils.isNotBlank(tool_id_fk)) {
				hql += " and tool_id_fk ='"+tool_id_fk+"'";
			}
			if (StringUtils.isNotBlank(wo_id_fk)) {
				hql += " and wo_id_fk ='"+wo_id_fk+"'";
			}
			List<Ret_ft_rule> list = null;
			try {
				list = retFutactionDao.findBySQL(hql,Ret_ft_rule.class);
			} catch (Exception e) {
				new RuntimeException(e);
			}

		if (list != null && list.size() > 0){
			outTrx.setTbl_cnt(list.size());
			outTrx.setRtn_code(RETURN_CODE_OK);
			outTrx.setOary(list);
		} else {
			outTrx.setRtn_code(RETURN_CODE_OK);
			outTrx.setTbl_cnt(0);
		}
	}

	private void registerByRes(FspfutationI inTrx, FspfutationO outTrx) {

			FspfutationIA fspfutationIA = inTrx.getIary().get(0);
			String res_mdl_id_fk = fspfutationIA.getRes_mdl_id_fk();
			String res_path_id_fk = fspfutationIA.getRes_path_id_fk();
			String res_path_ver_fk = fspfutationIA.getRes_path_ver_fk();
			String res_ope_id_fk = fspfutationIA.getRes_ope_id_fk();
			String res_ope_no_fk = fspfutationIA.getRes_ope_no_fk();
			String res_tool_id_fk = fspfutationIA.getRes_tool_id_fk();
			String res_wo_id_fk = fspfutationIA.getRes_wo_id_fk();

			String act_pos_flg = fspfutationIA.getAct_pos_flg();
			String deft_code = fspfutationIA.getDeft_code();
			String group_id_fk = fspfutationIA.getGroup_id_fk();
			String mdl_id_fk = fspfutationIA.getMdl_id_fk();
			String ope_id_fk = fspfutationIA.getOpe_id_fk();
			String ope_no_fk = fspfutationIA.getOpe_no_fk();
			String path_id_fk = fspfutationIA.getPath_id_fk();
			String path_ver_fk = fspfutationIA.getPath_ver_fk();
			String prd_grade = fspfutationIA.getPrd_grade();
			String rsn_code = fspfutationIA.getRsn_code();
			String rul_act_code = fspfutationIA.getRul_act_code();
			String tool_id_fk = fspfutationIA.getTool_id_fk();
			String wo_id_fk = fspfutationIA.getWo_id_fk();
			String prd_seq_id = fspfutationIA.getPrd_seq_id();
			String ope_note = fspfutationIA.getOpe_note();
			String[] slot_arr = fspfutationIA.getSlot_arr();
			String evt_usr = fspfutationIA.getEvt_usr();
			//唯一码支持多选
		String replace = prd_seq_id.replace("\t", "\n");
		String[] prd_seq_arr = replace.split("\n");


			if ("FTHL".equals(rul_act_code)) {
				boolean exitOne = isExitOne(rul_act_code,mdl_id_fk,prd_seq_id,path_id_fk,path_ver_fk,ope_id_fk,
						ope_no_fk,tool_id_fk,group_id_fk,prd_grade,wo_id_fk,act_pos_flg);
				if (exitOne == true) {
					if (prd_seq_arr.length > 0) {
						for (int i = 0; i < prd_seq_arr.length; i++) {
							if (StringUtils.isNotBlank(prd_seq_arr[i])){
								saveToRul(mdl_id_fk,1,path_id_fk,path_ver_fk,ope_id_fk,ope_no_fk,tool_id_fk,deft_code,group_id_fk,prd_grade,
										rsn_code,rul_act_code,wo_id_fk,act_pos_flg,"N",ope_note,res_mdl_id_fk,res_ope_id_fk,res_ope_no_fk,res_path_id_fk,
										res_path_ver_fk,res_tool_id_fk,res_wo_id_fk,prd_seq_arr[i],evt_usr);
							}

						}
					} else {
						saveToRul(mdl_id_fk,1,path_id_fk,path_ver_fk,ope_id_fk,ope_no_fk,tool_id_fk,deft_code,group_id_fk,prd_grade,
								rsn_code,rul_act_code,wo_id_fk,act_pos_flg,"N",ope_note,res_mdl_id_fk,res_ope_id_fk,res_ope_no_fk,res_path_id_fk,
								res_path_ver_fk,res_tool_id_fk,res_wo_id_fk,null,evt_usr);
					}
					outTrx.setRtn_code(RETURN_CODE_OK);
				} else {
					outTrx.setRtn_code(RETURN_CODE_SET_ERROR);
					outTrx.setRtn_mesg("类型：预约保留，业务主键已存在一笔");
				}
			} else if ("FTAJ".equals(rul_act_code)) {
				boolean exitOne = isExitOne(rul_act_code,mdl_id_fk,prd_seq_id,path_id_fk,path_ver_fk,ope_id_fk,
						ope_no_fk,tool_id_fk,group_id_fk,prd_grade,wo_id_fk,act_pos_flg);
				if (exitOne == true) {
					if (prd_seq_arr.length > 0) {
						for (int i = 0; i < prd_seq_arr.length; i++) {
							if (prd_seq_arr[i] != "") {
								saveToRul(mdl_id_fk, 2, path_id_fk, path_ver_fk, ope_id_fk, ope_no_fk, tool_id_fk, deft_code, group_id_fk, prd_grade,
										rsn_code, rul_act_code, wo_id_fk, act_pos_flg, "N", ope_note, res_mdl_id_fk, res_ope_id_fk, res_ope_no_fk, res_path_id_fk,
										res_path_ver_fk, res_tool_id_fk, res_wo_id_fk, prd_seq_arr[i],evt_usr);
							}
						}
					} else {
						saveToRul(mdl_id_fk,1,path_id_fk,path_ver_fk,ope_id_fk,ope_no_fk,tool_id_fk,deft_code,group_id_fk,prd_grade,
								rsn_code,rul_act_code,wo_id_fk,act_pos_flg,"N",ope_note,res_mdl_id_fk,res_ope_id_fk,res_ope_no_fk,res_path_id_fk,
								res_path_ver_fk,res_tool_id_fk,res_wo_id_fk,null,evt_usr);
					}
					outTrx.setRtn_code(RETURN_CODE_OK);
				} else {
					outTrx.setRtn_code(RETURN_CODE_SET_ERROR);
					outTrx.setRtn_mesg("类型：预约调整，业务主键已存在一笔");
				}
			} else if ("FTRW".equals(rul_act_code)){
				if (prd_seq_arr.length > 0) {
					for (int i = 0; i < prd_seq_arr.length; i++) {
						if (prd_seq_arr[i] != "") {
							String rul_seq_id = UUID.randomUUID().toString().replace("-", "").substring(0, 25);
							Integer byPrima = findByPrima(rul_act_code, mdl_id_fk, prd_seq_id, path_id_fk, path_ver_fk, ope_id_fk,
									ope_no_fk, tool_id_fk, group_id_fk, prd_grade, wo_id_fk, act_pos_flg);
							saveToRul(mdl_id_fk, byPrima, path_id_fk, path_ver_fk, ope_id_fk, ope_no_fk, tool_id_fk, deft_code, group_id_fk, prd_grade,
									rsn_code, rul_act_code, wo_id_fk, act_pos_flg, "N", ope_note, res_mdl_id_fk, res_ope_id_fk, res_ope_no_fk, res_path_id_fk,
									res_path_ver_fk, res_tool_id_fk, res_wo_id_fk, prd_seq_arr[i],evt_usr);
							saveToSlot(slot_arr, rul_seq_id, evt_usr, ope_note, 2);
						}
					}
				} else {
					String rul_seq_id = UUID.randomUUID().toString().replace("-", "").substring(0, 25);
					Integer byPrima = findByPrima(rul_act_code, mdl_id_fk, prd_seq_id, path_id_fk, path_ver_fk, ope_id_fk,
							ope_no_fk, tool_id_fk, group_id_fk, prd_grade, wo_id_fk, act_pos_flg);
					saveToRul(mdl_id_fk,byPrima,path_id_fk,path_ver_fk,ope_id_fk,ope_no_fk,tool_id_fk,deft_code,group_id_fk,prd_grade,
							rsn_code,rul_act_code,wo_id_fk,act_pos_flg,"N",ope_note,res_mdl_id_fk,res_ope_id_fk,res_ope_no_fk,res_path_id_fk,
							res_path_ver_fk,res_tool_id_fk,res_wo_id_fk,null,evt_usr);
					saveToSlot(slot_arr,rul_seq_id,evt_usr,ope_note,2);
				}
				outTrx.setRtn_code(RETURN_CODE_OK);
			} else if ("FTSM".equals(rul_act_code)) {
				if (prd_seq_arr.length > 0) {
					for (int i = 0; i < prd_seq_arr.length; i++) {
						if (prd_seq_arr[i] != "") {
							String rul_seq_id = UUID.randomUUID().toString().replace("-", "").substring(0, 25);
							Integer byPrima = findByPrima(rul_act_code, mdl_id_fk, prd_seq_id, path_id_fk, path_ver_fk, ope_id_fk,
									ope_no_fk, tool_id_fk, group_id_fk, prd_grade, wo_id_fk, act_pos_flg);
							saveToRul(mdl_id_fk, byPrima, path_id_fk, path_ver_fk, ope_id_fk, ope_no_fk, tool_id_fk, deft_code, group_id_fk, prd_grade,
									rsn_code, rul_act_code, wo_id_fk, act_pos_flg, "N", ope_note, res_mdl_id_fk, res_ope_id_fk, res_ope_no_fk, res_path_id_fk,
									res_path_ver_fk, res_tool_id_fk, res_wo_id_fk, prd_seq_arr[i],evt_usr);
							saveToSlot(slot_arr, rul_seq_id, evt_usr, ope_note, 3);
						}
					}
				} else {

					String rul_seq_id = UUID.randomUUID().toString().replace("-", "").substring(0, 25);

					Integer byPrima = findByPrima(rul_act_code, mdl_id_fk, prd_seq_id, path_id_fk, path_ver_fk, ope_id_fk,
							ope_no_fk, tool_id_fk, group_id_fk, prd_grade, wo_id_fk, act_pos_flg);
					saveToRul(mdl_id_fk,byPrima,path_id_fk,path_ver_fk,ope_id_fk,ope_no_fk,tool_id_fk,deft_code,group_id_fk,prd_grade,
							rsn_code,rul_act_code,wo_id_fk,act_pos_flg,"N",ope_note,res_mdl_id_fk,res_ope_id_fk,res_ope_no_fk,res_path_id_fk,
							res_path_ver_fk,res_tool_id_fk,res_wo_id_fk,null,evt_usr);
					saveToSlot(slot_arr,rul_seq_id,evt_usr,ope_note,3);
				}

				outTrx.setRtn_code(RETURN_CODE_OK);
			}


		}

	private void active(FspfutationI inTrx, FspfutationO outTrx) {


			FspfutationIA fspfutationIA = inTrx.getIary().get(0);
			String rul_seq_id = fspfutationIA.getRul_seq_id();
			Ret_ft_rule ret_ft_rule = retFutactionDao.get(rul_seq_id);
			if ("N".equalsIgnoreCase(ret_ft_rule.getValid_flg())){
				ret_ft_rule.setValid_flg("Y");
				retFutactionDao.update(ret_ft_rule);
				outTrx.setRtn_mesg("已经激活");
				outTrx.setRtn_code(RETURN_CODE_OK);
			} else {
				ret_ft_rule.setValid_flg("N");
				retFutactionDao.update(ret_ft_rule);
				outTrx.setRtn_mesg("已经冻结");
				outTrx.setRtn_code(RETURN_CODE_OK);
			}


	}
	private void deleteBySeqId(FspfutationI inTrx, FspfutationO outTrx) {
		FspfutationIA fspfutationIA = inTrx.getIary().get(0);
		String[] rul_seq_ids = fspfutationIA.getRul_seq_ids();
		for (String rul_seq_id : rul_seq_ids) {
			retFutactionDao.deleteByKey(rul_seq_id);
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
	}


	public void getReqExcelEntityProc(String fileName,InputStream fileInputStream){

		try {
			boolean is03Excel = fileName.matches("^.+\\.(?i)(xls)$");
			//1、读取工作簿
			Workbook workbook = is03Excel ? new HSSFWorkbook(fileInputStream):new XSSFWorkbook(fileInputStream);
			//2、读取工作表
			Sheet sheet = workbook.getSheetAt(0);
			//3、读取行
			//4,存入数组
			ArrayList<String> arrayList = new ArrayList<>();
			if(sheet.getPhysicalNumberOfRows() > 2){
				for(int k = 1; k < sheet.getPhysicalNumberOfRows(); k++){
					//4、读取单元格
					Row row = sheet.getRow(k);
					//用户名
					Cell cell0 = row.getCell(0);
					arrayList.add(cell0.getStringCellValue());
				}
			}
			fileInputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private boolean isExitOne(String rul_act_code,String mdl_id_fk,String prd_seq_id,String path_id_fk,String path_ver_fk,String ope_id_fk,String ope_no_fk,
							  String tool_id_fk,String group_id_fk,String prd_grade,String wo_id_fk,String act_pos_flg) {

		String hql = "select * from ret_ft_rule where 1 = 1 ";
		/*if (StringUtils.isNotBlank(rul_act_code)) {
			hql += " and rul_act_code ='"+rul_act_code+"'";
		} else {
			hql += " and rul_act_code is null";
		}
		if (StringUtils.isNotBlank(mdl_id_fk)) {
			hql += " and mdl_id_fk ='"+mdl_id_fk+"'";
		}else {
			hql += " and mdl_id_fk is null";
		}*/
		if (StringUtils.isNotBlank(prd_seq_id)) {
			hql += " and prd_seq_id_fk ='"+prd_seq_id+"'";
		}else {
			hql += " and prd_seq_id_fk is null";
		}
		/*if (StringUtils.isNotBlank(path_id_fk)) {
			hql += " and path_id_fk ='"+path_id_fk+"'";
		}else {
			hql += " and path_id_fk is null";
		}
		if (StringUtils.isNotBlank(path_ver_fk)) {
			hql += " and path_ver_fk ='"+path_ver_fk+"'";
		}else {
			hql += " and path_ver_fk is null";
		}
		if (StringUtils.isNotBlank(ope_id_fk)) {
			hql += " and ope_id_fk ='"+ope_id_fk+"'";
		}else {
			hql += " and ope_id_fk is null";
		}
		if (StringUtils.isNotBlank(ope_no_fk)) {
			hql += " and ope_no_fk ='"+ope_no_fk+"'";
		}else {
			hql += " and ope_no_fk is null";
		}
		if (StringUtils.isNotBlank(tool_id_fk)) {
			hql += " and tool_id_fk ='"+tool_id_fk+"'";
		}else {
			hql += " and tool_id_fk is null";
		}
		if (StringUtils.isNotBlank(group_id_fk)) {
			hql += " and group_id_fk ='"+group_id_fk+"'";
		}else {
			hql += " and group_id_fk is null";
		}
		if (StringUtils.isNotBlank(prd_grade)) {
			hql += " and prd_grade ='"+prd_grade+"'";
		}else {
			hql += " and prd_grade is null";
		}
		if (StringUtils.isNotBlank(wo_id_fk)) {
			hql += " and wo_id_fk ='"+wo_id_fk+"'";
		}else {
			hql += " and wo_id_fk is null";
		}
		if (StringUtils.isNotBlank(act_pos_flg)) {
			hql += " and act_pos_flg ='"+act_pos_flg+"'";
		}else {
			hql += " and act_pos_flg is null";
		}*/
		List<Ret_ft_rule> ret_ft_ruleList = retFutactionDao.findBySQL(hql, Ret_ft_rule.class);
		if (ret_ft_ruleList.size() >= 1){
			return false;
		}
		return true;
	}
	private Integer findByPrima(String rul_act_code,String mdl_id_fk,String prd_seq_id,String path_id_fk,String path_ver_fk,String ope_id_fk,String ope_no_fk,
							  String tool_id_fk,String group_id_fk,String prd_grade,String wo_id_fk,String act_pos_flg) {

		String hql = "select * from ret_ft_rule where 1 = 1 and rul_act_code in ('FTRW','FTSM') ";
		if (StringUtils.isNotBlank(mdl_id_fk)) {
			hql += " and mdl_id_fk ='"+mdl_id_fk+"'";
		}
		if (StringUtils.isNotBlank(prd_seq_id)) {
			hql += " and prd_seq_id_fk ='"+prd_seq_id+"'";
		}
		if (StringUtils.isNotBlank(path_id_fk)) {
			hql += " and path_id_fk ='"+path_id_fk+"'";
		}
		if (StringUtils.isNotBlank(path_ver_fk)) {
			hql += " and path_ver_fk ='"+path_ver_fk+"'";
		}
		if (StringUtils.isNotBlank(ope_id_fk)) {
			hql += " and ope_id_fk ='"+ope_id_fk+"'";
		}
		if (StringUtils.isNotBlank(ope_no_fk)) {
			hql += " and ope_no_fk ='"+ope_no_fk+"'";
		}
		if (StringUtils.isNotBlank(tool_id_fk)) {
			hql += " and tool_id_fk ='"+tool_id_fk+"'";
		}
		if (StringUtils.isNotBlank(group_id_fk)) {
			hql += " and group_id_fk ='"+group_id_fk+"'";
		}
		if (StringUtils.isNotBlank(prd_grade)) {
			hql += " and prd_grade ='"+prd_grade+"'";
		}
		if (StringUtils.isNotBlank(wo_id_fk)) {
			hql += " and wo_id_fk ='"+wo_id_fk+"'";
		}
		if (StringUtils.isNotBlank(act_pos_flg)) {
			hql += " and act_pos_flg ='"+act_pos_flg+"'";
		}
		hql += " order by rul_act_prty desc";
		List<Ret_ft_rule> ret_ft_ruleList = retFutactionDao.findBySQL(hql, Ret_ft_rule.class);
		Integer rul_act_prty;
		if (ret_ft_ruleList.size() >= 1){
			rul_act_prty = ret_ft_ruleList.get(0).getRul_act_prty();
		} else {
			rul_act_prty = 1;
		}
		return rul_act_prty;
	}
	private void saveToSlot(String[] slot_arr,String rul_seq_id,String evt_usr,String ope_note,Integer Reser_type) {
		if (slot_arr.length > 0){
			for (int i = 0; i < slot_arr.length; i++) {
				RetSlotDef retSlotDef = new RetSlotDef();
				String slot_seq_id = UUID.randomUUID().toString().replace("-", "").substring(0, 25);
				String un_seq_id = UUID.randomUUID().toString().replace("-", "").substring(0, 25);
				retSlotDef.setSlot_seq_id(slot_seq_id);
				retSlotDef.setUnq_seq_id(un_seq_id);
				retSlotDef.setSlot_set_typ(Reser_type);
				retSlotDef.setSlot_get_method(1);
				retSlotDef.setAct_seq_id(rul_seq_id);
				retSlotDef.setSlot_no(slot_arr[i]);
				retSlotDef.setCrt_usr(evt_usr);
				retSlotDef.setCrt_timestamp(new Timestamp(new Date().getTime()));
				retSlotDef.setCrt_note(ope_note);
				retSlotDefDao.saveOrUpdate(retSlotDef);
			}

		}
	}
	private void saveToRul(String mdl_id_fk, int i, String path_id_fk, String path_ver_fk, String ope_id_fk,
						   String ope_no_fk, String tool_id_fk, String deft_code, String group_id_fk, String prd_grade,
						   String rsn_code, String rul_act_code, String wo_id_fk, String act_pos_flg, String n, String ope_note,
						   String res_mdl_id_fk, String res_ope_id_fk, String res_ope_no_fk, String res_path_id_fk,
						   String res_path_ver_fk, String res_tool_id_fk, String res_wo_id_fk, String s,String evtUsr) {
		Timestamp evt_timestamp = Util.getCurrentTimestamp();
		Ret_ft_rule ret_ft_rule = new Ret_ft_rule();
		String unq_seq_id = UUID.randomUUID().toString().replace("-", "").substring(0, 25);
		ret_ft_rule.setMdl_id_fk(mdl_id_fk);                       //P
		ret_ft_rule.setUnq_seq_id(unq_seq_id);
		ret_ft_rule.setRul_act_prty(1);
		ret_ft_rule.setPath_id_fk(path_id_fk);                       //P
		ret_ft_rule.setPath_ver_fk(path_ver_fk);                       //P
		ret_ft_rule.setOpe_id_fk(ope_id_fk);                       //P
		ret_ft_rule.setOpe_no_fk(ope_no_fk);                       //P
		ret_ft_rule.setTool_id_fk(tool_id_fk);                       //P
		ret_ft_rule.setDeft_code(deft_code);
		ret_ft_rule.setGroup_id_fk(group_id_fk);                       //P
		ret_ft_rule.setPrd_grade(prd_grade);                        //P
		ret_ft_rule.setRsn_code(rsn_code);
		ret_ft_rule.setRul_act_code(rul_act_code);
		ret_ft_rule.setWo_id_fk(wo_id_fk);                  //P
		ret_ft_rule.setAct_pos_flg(act_pos_flg);            //P
		ret_ft_rule.setValid_flg("N");
		ret_ft_rule.setOpe_note(ope_note);

		ret_ft_rule.setTo_mdl_id_fk(res_mdl_id_fk);
		ret_ft_rule.setTo_ope_id_fk(res_ope_id_fk);
		ret_ft_rule.setTo_ope_no_fk(res_ope_no_fk);
		ret_ft_rule.setTo_path_id_fk(res_path_id_fk);
		ret_ft_rule.setTo_path_ver_fk(res_path_ver_fk);
		ret_ft_rule.setTo_tool_id_fk(res_tool_id_fk);
		ret_ft_rule.setTo_wo_id_fk(res_wo_id_fk);
		String rul_seq_id = UUID.randomUUID().toString().replace("-", "").substring(0, 25);
		ret_ft_rule.setRul_seq_id(rul_seq_id);
		ret_ft_rule.setRul_group_id(rul_seq_id);
		ret_ft_rule.setPrd_seq_id_fk(s); //P
		ret_ft_rule.setEvt_usr(evtUsr);
		ret_ft_rule.setEvt_timestamp(evt_timestamp);
		retFutactionDao.save(ret_ft_rule);

	}

}
